import Core.WindFunctions as Wind
import Core.Gadget as Gadget
import Core.DataSeries as DataSeries
from datetime import *
import copy

def Request_Company_Info(database,databaseName,collectionName):

    symbols = []
    #symbols.append("000001.SZ")

    instruments = database.findAll("Instruments","Stock")
    for instrument in instruments:
        symbols.append(instrument["Symbol"])

    w.start() # 启动Wind API
    data = w.wss(symbols, "comp_name,briefing,business,majorproducttype,majorproductname")
    for i in range(symbols.__len__()):
        symbol = symbols[i]
        dataObject = {}
        dataObject["Properties"] = {}
        dataObject["Properties"]["CompName"] = data.Data[0][i]
        dataObject["Properties"]["Business"] = data.Data[1][i]
        dataObject["Properties"]["Briefing"] = data.Data[2][i]
        dataObject["Properties"]["MajorProductType"] = data.Data[3][i]
        dataObject["Properties"]["MajorProductName"] = data.Data[4][i]

        database.update(databaseName, collectionName,{"Symbol":symbol}, dataObject)
        print("Build Company Info: " + dataObject["Properties"]["CompName"])
        #insert Target
        #update = {}
        #update["$set"] = dataObject
        #db = database.client[databaseName]
        #coll =  db[collectionName]
        #coll.update_one({"Symbol":symbol}, update, upsert=True)


#---更新证券名称---
def UpdateInstrumentName(database, datetime0):
    #
    instruments = database.findWithFilter("Instruments", "Stock", {})
    instruments2 = []
    for instrument in instruments:
        if instrument["Description"] == "":
            print(instrument["Symbol"])
            instruments2.append(instrument["Symbol"])

    #
    data = Wind.WSS(instruments2, "sec_name")
    for i in range(data.Data[0].__len__()):
        name = data.Data[0][i]
        symbol = data.Codes[i]
        database.addItem("Instruments", "Stock", {"Symbol": symbol}, {"Description": name})
    kkwood = 0